package com.stone.zk.lock.api.controller.v1;

import com.stone.zk.lock.app.service.ZookeeperLockService;
import com.stone.zk.lock.config.ZookeeperConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * zk分布式锁测试
 *
 * @author Mr_wenpan@163.com 2021/08/02 14:02
 */
@Slf4j
@RestController("zkLockTestController.v1")
@RequestMapping("/v1/{organizationId}/zk-lock-test")
public class ZkLockTestController {

    @Autowired
    private ZookeeperConfig zookeeperConfig;

    @RequestMapping("zkLock")
    public String testLock() {
        log.debug("进入testLock()方法；");
        //“order"表示业务模块
        try (ZookeeperLockService lock = new ZookeeperLockService(zookeeperConfig)) {
            if (lock.getLock("order")) {
                log.debug("获取到锁；");
                Thread.sleep(20 * 1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.debug("方法执行完成；");
        return "方法执行完成";
    }

}
